<script setup>
import { defineProps } from 'vue';
import defaultAvatar from '@/assets/home/avatar1.jpg';

const props = defineProps({
  comment: {
    type: Object,
    required: true
  }
});

// 格式化时间
const formatDate = (dateString) => {
  if (!dateString) return '未知时间';
  
  try {
    const date = new Date(dateString);
    if (isNaN(date.getTime())) return dateString;
    
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: 'numeric',
      day: 'numeric',
      hour: '2-digit',
      minute: '2-digit'
    });
  } catch (e) {
    console.error('日期格式化错误:', e);
    return dateString;
  }
};
</script>

<template>
  <div class="comment-item">
    <div class="comment-avatar">
      <img 
        :src="comment.avatarUrl || defaultAvatar" 
        :alt="comment.nickname || '用户头像'"
        @error="$event.target.src = defaultAvatar"
        loading="lazy"
      >
    </div>
    <div class="comment-content">
      <div class="comment-header">
        <span class="comment-author">{{ comment.nickname || comment.username || `用户${comment.userId}` }}</span>
        <span class="comment-time">{{ formatDate(comment.createTime) }}</span>
      </div>
      <div class="comment-text rich-content" v-html="comment.content"></div>
    </div>
  </div>
</template>

<style scoped>
.comment-item {
  display: flex;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.comment-item:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.comment-avatar {
  width: 36px;
  height: 36px;
  margin-right: 12px;
  border-radius: 50%;
  overflow: hidden;
}

.comment-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.comment-content {
  flex: 1;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
}

.comment-author {
  font-weight: 500;
  color: #333;
  font-size: 0.9rem;
}

.comment-time {
  color: #999;
  font-size: 0.8rem;
}

.comment-text {
  font-size: 0.95rem;
  color: #555;
  line-height: 1.5;
}

/* 富文本内容样式 */
.rich-content :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin: 4px 0;
}

.rich-content :deep(a) {
  color: #00c389;
  text-decoration: none;
}

.rich-content :deep(a:hover) {
  text-decoration: underline;
}

.rich-content :deep(p) {
  margin: 0.25em 0;
}

.rich-content :deep(ul), .rich-content :deep(ol) {
  padding-left: 16px;
  margin: 0.25em 0;
}

.rich-content :deep(blockquote) {
  border-left: 3px solid #e0e0e0;
  padding-left: 12px;
  margin: 0.25em 0;
  color: #777;
}

.rich-content :deep(h1), .rich-content :deep(h2), .rich-content :deep(h3), 
.rich-content :deep(h4), .rich-content :deep(h5), .rich-content :deep(h6) {
  margin: 0.5em 0 0.25em;
  color: #333;
  font-size: 1em;
}
</style> 